const Common = require('../util/common')
const Constant = require('../constant/constant')
const moment = require('moment');
const RemarkModel = require('../model/remark')
const db = require('../db');
const { Op } = require("sequelize");


let exportObj = {
    createRemark,
    createRemarks,
    getShareRemark,
    getRemarks
};
// 导出对象，供其它模块调用
module.exports = exportObj;

//创建评论
async function createRemark(req, res) {
    // 定义一个返回对象
    if (Common.check(req.body, ['s_id','content']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            const result = await RemarkModel.create({
                u_id: req.user.id,
                s_id: req.body.s_id,
                content: req.body.content,
                like: 0,
                created_id: req.user.u_id,
                updated_id: req.user.u_id,
                created_at: moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'),
                updated_at: moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')
            })
            console.log(result)
            if (result.id) {
                res.json(resObj)
            } else {
                res.json(Constant.DEFAULT_ERROR)
            }
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}

//评论文章下面的评论
async function createRemarks(req, res) {
    // 定义一个返回对象
    if (Common.check(req.body, ['content', 'r_id','s_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            //开启事务托管自动处理提交事务和事务回滚
            return await db.transaction(async (t) => {
                let createRes = await RemarkModel.create({
                    u_id: req.user.id,
                    s_id: req.body.s_id,
                    content: req.body.content,
                    like: 0,
                    created_id: req.body.u_id,
                    updated_id: req.body.u_id,
                    created_at: moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'),
                    updated_at: moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')
                }, { transaction: t })
                if (createRes) {
                    res.json(resObj)
                } else {
                    res.json(Constant.DEFAULT_ERROR)
                }
            });
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}

//分页获取动态下评论
async function getShareRemark(req, res) {
    // 定义一个返回对象
    if (Common.check(req.query, ['s_id','page','limit']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            return await db.transaction(async (t) => {
                let result = await RemarkModel.findAll({
                    where:{
                        s_id : req.query.s_id,
                        r_id:{[Op.is]: null},
                    },
                    offset:(req.query.page - 1) * req.query.limit,
                    limit:parseInt(req.query.limit)
                },{transaction :t })
                resObj.data = result
                res.json(resObj)
            });
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}
//分页获取评论下评论
async function getRemarks(req, res) {
      // 定义一个返回对象
      if (Common.check(req.query, ['r_id','page','limit']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            return await db.transaction(async (t) => {
                let queryRes = await RemarkModel.findAll({
                    where:{
                        r_id: req.query.r_id,
                    },
                    offset:(req.query.page - 1) * req.query.limit,
                    limit:parseInt(req.query.limit)
                },{transaction :t })
                resObj.data = queryRes
                res.json(resObj)
            });
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}
